package com.example.project_dish.Capital;

import static com.example.project_dish.method.return_main;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.TextPaint;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.project_dish.Order.MyOrderActivity;
import com.example.project_dish.R;
import com.example.project_dish.Dishes.SelectDishActivity;
import com.example.project_dish.method;

import java.util.ArrayList;
import java.util.List;

public class CapitalActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView store_name;
    private TextView icon_text;
    private ImageView top_image;
    private ImageView add;
    private EditText city_text;
    private EditText commentText;
    private TextView test;
    private Button select_dish;
    private Button capital_dish;
    private Button order_dish;

    private View commentLayout;

    private List<Comment> data;
    private AdapterComment adapterComment;
    private ListView commentList;
    private Button comment_send;
    private EditText comment_content;

    private View alertDialogView;
    private String userName;
    private String userContent;
    private EditText userNameEdit;
    private EditText userContentEdit;
    private RadioGroup userStar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capital);

        store_name = findViewById(R.id.store_name);
        icon_text = findViewById(R.id.icon_text);
        // 设置背景图片透明度
        top_image = findViewById(R.id.top_image);
        method.setBackground(null,top_image);
        // 设置状态栏透明
        method.status_bar(this);
        if(this.getSupportActionBar() != null){
            this.getActionBar().hide();
        }
        //设置文字粗细
        TextPaint paint = store_name.getPaint();
        paint.setStrokeWidth(30f);
        // 设置图标大小
        Drawable drawableLeft = getResources().getDrawable(R.drawable.star);
        drawableLeft.setBounds(0,0,35,35);
        icon_text.setCompoundDrawables(drawableLeft,null,null,null);

        //获取按钮
        select_dish = findViewById(R.id.select_dish);
        select_dish.setOnClickListener(this);
        capital_dish = findViewById(R.id.capital_dish);
        capital_dish.setOnClickListener(this);
        order_dish = findViewById(R.id.order_dish);
        order_dish.setOnClickListener(this);
        // 设置部分字体颜色
        String icon_text_str = "<font color='#f2cb51'>4.6</font> 月售3000 商家十二分钟内··";
        icon_text.setText(Html.fromHtml(icon_text_str));

        initView();

        //调用双击功能函数
        return_main(top_image);

    }

    private void initView() {
        // 初始化评论列表
        commentList = (ListView) findViewById(R.id.comment_list);
        // 初始化数据
        data = new ArrayList<>();
        // 初始化适配器
        adapterComment = new AdapterComment(getApplicationContext(), data);
        // 为评论列表设置适配器
        commentList.setAdapter(adapterComment);

        comment_send = (Button) findViewById(R.id.comment_send);
        comment_send.setOnClickListener(this);

        Comment comment1 = new Comment();
        comment1.setName("用户:张三");
        Drawable drawable = getDrawable(R.drawable.star5);
        comment1.setStar(drawable);
        comment1.setContent("Things in this shop are delicious and I will take my friend to review it again next time");
        adapterComment.addComment(comment1);

        Comment comment2 = new Comment();
        comment2.setName("用户:李四");
        comment2.setStar(drawable);
        comment2.setContent("很好吃环境也很好，菜品也很多，下次会继续回顾的");
        adapterComment.addComment(comment2);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.select_dish:
                Intent intent = new Intent(CapitalActivity.this, SelectDishActivity.class);
                startActivity(intent);
                break;
            case R.id.capital_dish:
                Intent intent1 = new Intent(CapitalActivity.this, CapitalActivity.class);
                startActivity(intent1);
                break;
            case R.id.order_dish:
                Intent intent2 = new Intent(CapitalActivity.this, MyOrderActivity.class);
                startActivity(intent2);
                break;
            case R.id.comment_send:
                alertDialogView = getLayoutInflater ().inflate (R.layout.alertdialogview, null, false);
                AlertDialog.Builder builder = new AlertDialog.Builder(CapitalActivity.this);
                String[] sexArry = new String[] { "好好吃", "好吃","一般" };
                final String[] userStar_text = {""};
                builder.setView(alertDialogView);
                builder.setSingleChoiceItems(sexArry, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        userStar_text[0] = sexArry[i];
                    }
                });
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        userNameEdit = alertDialogView.findViewById (R.id.city_text);
                        userContentEdit = alertDialogView.findViewById (R.id.nian_text);
                        userName = userNameEdit.getText ().toString ();
                        userContent = userContentEdit.getText ().toString ();
                        if (userName.equals("") || userContent.equals("")){
                            Toast.makeText(getApplicationContext(), "评论不能为空！", Toast.LENGTH_SHORT).show();
                        }else {
                            // 生成评论数据
                            Comment comment = new Comment();
                            comment.setName("用户"+userName);
                            Drawable drawable = getDrawable(R.drawable.star5);
                            if (userStar_text[0].equals("好好吃")){
                                drawable = getDrawable(R.drawable.star4);
                            }else if(userStar_text[0].equals("好吃")){
                                drawable = getDrawable(R.drawable.star4);
                            }else if(userStar_text[0].equals("一般")){
                                drawable = getDrawable(R.drawable.star3);
                            }
                            comment.setStar(drawable);
                            comment.setContent(userContent);
                            adapterComment.addComment(comment);
                            Toast.makeText(getApplicationContext(), "评论成功！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.show();
        }
    }
}